<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Drorder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['order_type'])) {
                    $where[] = ['o.order_type', '=', $filter['order_type']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['h.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['h.city', '=', $city['name']];
                }
                if (isset($filter['hospital_name'])) {
                    $where[] = ['h.name', 'like', '%' . $filter['hospital_name'] . '%'];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['time'])) {
                    $create_time = explode(' - ', $filter['time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('driver_order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->order('o.pay_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }
                $_list[$k]['driver_name'] = "------";
                if ($v['driver_id']) {
                    $pz_info = Db::name('driver')->find($v['driver_id']);
                    if ($pz_info) {
                        $_list[$k]['driver_name'] = $pz_info['name'];
                    }
                }
                if ($v['go_time']) {
                    $_list[$k]['go_time'] = date('Y-m-d H:i:s', $v['go_time']);
                } else {
                    $_list[$k]['go_time'] = "------";
                }
                //1单程 2返程 3往返
                if ($v['order_type']==1){
                    $_list[$k]['driver_route']=$v['province'].$v['city'].$v['address'].'=========>>'.$v['hospital_name'];
                }elseif ($v['order_type']==2){
                    $_list[$k]['driver_route']=$v['province'].$v['city'].$v['address'].'<<========='.$v['hospital_name'];
                }else{
                    $_list[$k]['driver_route']=$v['province'].$v['city'].$v['address'].'<<=========>>'.$v['hospital_name'];
                }
            }
            //获取数量
            $total = Db::name('driver_order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $stmt = Db::name('driver_order')->where('id', $data['id'])->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('driver_order')->find($id);
        $this->assign('data', $data);
        return $this->fetch('');

    }

    public function dispatch(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['dispatch_time']=time();
            $data['row']['dispatch_type']=2;
            $data['row']['status']=7;
            $stmt = Db::name('driver_order')->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('driver_order')->find($id);
        $driver_list=Db::name('driver')->where([
            'status'=>1,
            'is_online'=>1,
        ])->select();
        $this->assign([
            'data'=>$data,
            'pz_list'=>$driver_list
        ]);
        return $this->fetch('');
    }

    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    //退款
    public function refund()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $order_info=Db::name('driver_order')->find($data['row']['id']);
            if ($data['row']['tui_price']<0){
                $this->error('请输入正确数值！');
            }
            if ($data['row']['tui_price']>$data['row']['pay_money']){
                $this->error('退款金额不能大于支付金额');
            }

            $config = config('wechat.wxpay');
            $app = Factory::payment($config);
            $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $data['row']['tui_price'] * 100, [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => '会员卡退款',
            ]);
            if ($result['result_code'] == "FAIL") {
                $this->error($result['err_code_des']);
            }
            tuikuanDriverOrder($order_info, $data['row']['tui_price'],1);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('driver_order')->find($id);
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('');
    }

}